import { store } from "@/stores";
import { defineStore } from "pinia";
import type { SelectGraphState } from "./helper";
import {
  defaultSetting,
  getLocalState,
  removeLocalState,
  setLocalState,
} from "./helper";

export const useSelectGraphStore = defineStore("select-graph-storage", {
  state: (): SelectGraphState => getLocalState(),
  getters: {
    getSelectCanvas(state) {
      return () => {
        return state.selectCanvas;
      };
    },
    getLockSelectCanvas(state) {
      return () => {
        return state.lockselectCanvas;
      };
    },
  },
  actions: {
    setSelectCanvas(canvas) {
      this.selectCanvas = canvas;

      this.recordState();
    },
    setLockSelectCanvas(canvas) {
      this.lockselectCanvas = canvas;

      this.recordState();
    },

    updateSelectGraph(selectGraph: Partial<SelectGraphState>) {
      this.$state = { ...this.$state, ...selectGraph };
      this.recordState();
    },

    resetSelectGraph() {
      this.$state = defaultSetting();
      removeLocalState();
    },

    recordState() {
      setLocalState(this.$state);
    },
  },
});

export function useSelectGraphStoreHook() {
  return useSelectGraphStore(store);
}
